<?php
class Shipping_ShipTypeCountryMapController extends Zend_Controller_Action
{
	public function indexAction(){

        if($this->_request->isPost()){
            $page = $this->_request->getParam('page',1);
    	    $pageSize = $this->_request->getParam('pageSize',20);
            $stcm_area = $this->_request->getParam('stcm_area','');
            $st_code = $this->_request->getParam('st_code','');
            $country_id = $this->_request->getParam('country_id','');
            $condition = array(
                'stcm_area'     =>$stcm_area,
                'st_code'       =>$st_code,
                'country_id'    =>$country_id,
            );

            $list= Shipping_Service_ShipTypeCountryMap::getByCondition($condition,'*','stcm_id,st_code,country_id,stcm_area',$page,$pageSize);
            $count = Shipping_Service_ShipTypeCountryMap::getByCondition($condition,'count(*)');
            $json =array('list'=>$list,'count'=>$count);
            echo json_encode($json);
            exit;

        }else{

            $shipTypeList  = Shipping_Service_ShipType::getByCondition(array('st_status'=>1));
            $countryList  = Common_Service_Country::getByCondition(null,'*','en_name,sort');
            $this->view->shipTypeList  = $shipTypeList;
            $this->view->countryList  = $countryList;
            echo Cff::renderTpl("shipping/shiptypecountrymap/index.tpl","layout");
        }
    }


    public function infoAction(){
        $stcm_id = $this->_request->getParam('stcm_id',0);
        try{
            $shipType = Shipping_Service_ShipTypeCountryMap::getByValue($stcm_id);
            $return = array(
                'ask' =>true,
                'list' =>$shipType,
            );
        }catch(Exception $e){
            $return = array(
                'ask' =>false,
                'list' =>null,
            );
        }
        echo json_encode($return);
        exit;
    }


    public function editAction(){
        $lang = Zend_Registry::get('lang');
        $return = array();
        $stcm_id = $this->_request->getParam('stcm_id',0);
        $stcm_area = $this->_request->getParam('stcm_area','');
        $st_code = $this->_request->getParam('st_code','');
        $country_id = $this->_request->getParam('country_id','');
        $country = Common_Service_Country::getByValue($country_id);
        $val =array(
            'stcm_id' =>$stcm_id,
            'stcm_area' =>$stcm_area,
            'st_code' =>$st_code,
            'country_id' =>$country_id,
            'country_code' =>@$country['country_code'],
            'country_name' =>@$country['cn_name'],
        );

        $error = Shipping_Service_ShipTypeCountryMap::validator($val);
        if(!empty($error)){
            $return['ask'] =false;
            $return['list']['error'] =$error;
            echo Zend_Json::encode($return);
            exit ;
        }
        unset($val['stcm_id']);
        if($stcm_id>0){

            if(Shipping_Service_ShipTypeCountryMap::update($val,$stcm_id)){
                $return['ask'] =true;
                $return['list']['message'] =$lang['operation'].$lang['success'];
            }else{
                $return['ask'] =false;
                $return['list']['error'][] =$lang['operation'].$lang['fail'];
            }
        }else{
            if(Shipping_Service_ShipTypeCountryMap::add($val)){
                $return['ask'] =true;
                $return['list']['message'][] =$lang['operation'].$lang['success'];
            }else{
                $return['ask'] =false;
                $return['list']['error'][] =$lang['operation'].$lang['fail'];
            }
        }
        echo Zend_Json::encode($return);
        exit ;
    }
	
    public function deleteAction(){
        $lang = Zend_Registry::get('lang');
        $return = array();
        $stcm_id = $this->_request->getParam('stcm_id',0);
        if(Shipping_Service_ShipTypeCountryMap::delete($stcm_id)){
            $return['ask'] =true;
            $return['list']['message'] = $lang['operation'].$lang['success'];
        }else{
            $return['ask'] =false;
            $return['list']['error'][] = $lang['operation'].$lang['fail'];
        }
        echo Zend_Json::encode($return);
        exit;
    }
	
}